package com.gzy.studentsociety.dao;

import com.gzy.studentsociety.entity.Member;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class MemberDaoImpl extends BaseDao implements MemberDao{
    @Override
    public Integer addMember(Member member) throws SQLException {
        String sql = "insert into member (memberStudentId,joinDate,member_societyId) value (?,?,?)";
        QueryRunner qr = new QueryRunner(getDataSource());
        return qr.update(sql,member.getMemberStudentId(),member.getJoinDate(),member.getMember_societyId());
    }
    @Override
    public Integer isMemberIn(Member member) throws SQLException {
        String sql = "select count(1) from member where member_societyId = ? and memberStudentId = ?";
        QueryRunner qr = new QueryRunner(getDataSource());
        return qr.query(sql, new ScalarHandler<Long>(),member.getMember_societyId(), member.getMemberStudentId()).intValue();
    }
    @Override
    public List<Member> queryAllMember(Integer status, Integer member_societyId) throws SQLException {
        String sql = "select * from member where memberStatus = ? and member_societyId = ?";
        QueryRunner qr = new QueryRunner(getDataSource());
        return qr.query(sql, new BeanListHandler<>(Member.class), status, member_societyId);
    }


}
